<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Classes;
use app\common\model\Grade;
use think\Validate;

/**
 * 学校
 *
 * @package app\api\controller
 */
class School extends Api
{
    protected $noNeedLogin = ['classes', 'teachers'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * @ApiTitle    (学校列表)
     * @ApiAuthor   (Zhenzh)
     * @ApiDate     (2020/1/15)
     * @ApiSummary  (描述)
     * @ApiBody     (Body)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/school/lists)
     * @ApiHeaders  (name="token", type="string", required=true, description="请求的Token")
     * @ApiReturnParams   (name="code", type="integer", required=true, description="状态码 1正常 0错误", sample="1")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回", sample="")
     * @ApiReturn ({
    "code": 1,
    "msg": "",
    "time": "1579067524",
    "data": [
    {
    "id": 36,
    "name": "房山长阳第一小学",
    "province_id": 1,
    "city_id": 2,
    "district_id": 10,
    "address": "长阳镇起步区3号",
    "logo": "http://mp.uxuj.test/uploads/20191118/b4037c02a840db9dd1a78fa6ca932e1e.jpg",
    "note": null,
    "school_id": 3,
    "admin_id": 0,
    "created_at": 1573873298
    }
    ]
    })
     */
    public function lists()
    {
        $list = \app\common\model\School::where('id', $this->auth->school_id)->select();
        $this->success('', $list);
    }

    /**
     * @ApiTitle    (学校基本信息)
     * @ApiAuthor   (Zhenzh)
     * @ApiDate     (2020/1/15)
     * @ApiSummary  (描述)
     * @ApiBody     (Body)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/school/profile)
     * @ApiHeaders  (name="token", type="string", required=true, description="请求的Token")
     * @ApiParams   (name="id", type="int", required=true, description="学校ID", sample="")
     * @ApiReturnParams   (name="code", type="integer", required=true, description="状态码 1正常 0错误", sample="1")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回", sample="")
     * @ApiReturn ({
    "code": 1,
    "msg": "",
    "time": "1579067665",
    "data": {
    "id": 36,
    "name": "房山长阳第一小学",
    "province_id": 1,
    "city_id": 2,
    "district_id": 10,
    "address": "长阳镇起步区3号",
    "logo": "http://mp.uxuj.test/uploads/20191118/b4037c02a840db9dd1a78fa6ca932e1e.jpg",
    "note": null,
    "school_id": 3,
    "admin_id": 0,
    "created_at": 1573873298
    }
    })
     */
    public function profile()
    {
        $id = $this->request->request('id', 0);
        $result = \app\common\model\School::where('id', $id)->find();
        $this->success('', $result);
    }

    /**
     * @ApiTitle    (学校详细信息)
     * @ApiAuthor   (Zhenzh)
     * @ApiDate     (2020/1/15)
     * @ApiSummary  (描述)
     * @ApiBody     (Body)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/school/detail)
     * @ApiHeaders  (name="token", type="string", required=true, description="请求的Token")
     * @ApiParams   (name="id", type="int", required=true, description="学校ID", sample="")
     * @ApiReturnParams   (name="code", type="integer", required=true, description="状态码 1正常 0错误", sample="1")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回", sample="")
     * @ApiReturn ({
    "code": 1,
    "msg": "",
    "time": "1579067685",
    "data": {
    "id": 36,
    "name": "房山长阳第一小学",
    "province_id": 1,
    "city_id": 2,
    "district_id": 10,
    "address": "长阳镇起步区3号",
    "logo": "http://mp.uxuj.test/uploads/20191118/b4037c02a840db9dd1a78fa6ca932e1e.jpg",
    "note": null,
    "school_id": 3,
    "admin_id": 0,
    "created_at": 1573873298,
    "student_count": 13,
    "classes_count": 5,
    "teacher_count": 10
    }
    })
     */
    public function detail()
    {
        $id = $this->request->request('id', 0);
        $result = \app\common\model\School::withCount('student,classes,teacher')
            ->where('id', $id)
            ->find();
        $this->success('', $result);
    }

    /**
     * @ApiTitle    (年级列表)
     * @ApiAuthor   (Zhenzh)
     * @ApiDate     (2020/1/15)
     * @ApiSummary  (描述)
     * @ApiBody     (Body)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/school/grades)
     * @ApiHeaders  (name="token", type="string", required=true, description="请求的Token")
     * @ApiParams   (name="id", type="int", required=true, description="学校ID", sample="")
     * @ApiReturnParams   (name="code", type="integer", required=true, description="状态码 1正常 0错误", sample="1")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回", sample="")
     * @ApiReturn ({
    "code": 1,
    "msg": "",
    "time": "1579068642",
    "data": [
    {
    "id": 1,
    "name": "一年级",
    "stage": 1,
    "key": 1
    },
    {
    "id": 2,
    "name": "二年级",
    "stage": 1,
    "key": 2
    },
    {
    "id": 3,
    "name": "三年级",
    "stage": 1,
    "key": 3
    },
    {
    "id": 4,
    "name": "四年级",
    "stage": 1,
    "key": 4
    },
    {
    "id": 5,
    "name": "五年级",
    "stage": 1,
    "key": 5
    },
    {
    "id": 6,
    "name": "六年级",
    "stage": 1,
    "key": 6
    },
    {
    "id": 7,
    "name": "初一",
    "stage": 2,
    "key": 7
    },
    {
    "id": 8,
    "name": "初二",
    "stage": 2,
    "key": 8
    },
    {
    "id": 9,
    "name": "初三",
    "stage": 2,
    "key": 9
    },
    {
    "id": 10,
    "name": "高一",
    "stage": 3,
    "key": 10
    },
    {
    "id": 11,
    "name": "高二",
    "stage": 3,
    "key": 11
    },
    {
    "id": 12,
    "name": "高三",
    "stage": 3,
    "key": 12
    },
    {
    "id": 13,
    "name": "学前班",
    "stage": 0,
    "key": 0
    },
    {
    "id": 14,
    "name": "其他",
    "stage": 14,
    "key": 13
    }
    ]
    })
     */
    public function grades()
    {
        $list = Grade::all();
        $this->success('', $list);
    }

    /**
     * @ApiTitle    (班级列表)
     * @ApiAuthor   (Zhenzh)
     * @ApiDate     (2020/1/15)
     * @ApiSummary  (描述)
     * @ApiBody     (Body)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/school/classes)
     * @ApiParams   (name="id", type="int", required=true, description="学校 ID", sample="36")
     * @ApiParams   (name="grade_id", type="int", required=false, description="年级 ID", sample="1")
     * @ApiReturnParams   (name="code", type="integer", required=true, description="状态码 1正常 0错误", sample="1")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回", sample="")
     * @ApiReturn ({
    "code": 1,
    "msg": "",
    "time": "1579071493",
    "data": [
    {
    "id": 1,
    "name": "一年级",
    "stage": 1,
    "key": 1,
    "classes": [
    {
    "id": 52,
    "name": "1班",
    "school_id": 36,
    "grade_id": 1,
    "classroom_id": 30,
    "headmaster": 42,
    "slogan": null,
    "logo": null,
    "school_id": 3,
    "created_at": 1573873470,
    "type": 1
    },
    {
    "id": 97,
    "name": "实验室",
    "school_id": 36,
    "grade_id": 1,
    "classroom_id": 35,
    "headmaster": 48,
    "slogan": "书上得来终觉浅,绝知此事要躬行",
    "logo": null,
    "school_id": 3,
    "created_at": 1574666014,
    "type": 1
    },
    {
    "id": 98,
    "name": "3班",
    "school_id": 36,
    "grade_id": 1,
    "classroom_id": 30,
    "headmaster": 42,
    "slogan": " 1234",
    "logo": "/uploads/20191126/8284af499ea84f5bb8517c815867e7c3.jpg",
    "school_id": 3,
    "created_at": 1574752789,
    "type": 1
    }
    ]
    }
    ]
    })
     */
    public function classes()
    {
        $id = $this->request->request('id', 0);
        $grade_id = $this->request->request('grade_id', 0);
        $keywords = $this->request->request('keywords', '');

        if (!$id) {
            $this->error('参数有误');
        }

        $list = Grade::with(['classes' => function ($query) use ($id, $keywords) {
            $query->where('school_id', $id);
            if ($keywords) {
                $query->whereLike('name', "%$keywords%");
            }
        }])
            ->where(function ($query) use ($grade_id) {
                if ($grade_id) {
                    $query->where('id', $grade_id);
                }
            })->select();

        if ($list) {
            foreach ($list as $key => $value) {
                foreach ($value['classes'] as $k => $class) {
                    if (!strstr($class['logo'], 'http') && !strpos($class['logo'], 'https')) {
                        if (strstr($class['logo'], '/uploads/')) {
                            $list[$key]['classes'][$k]['logo'] = config('app_url') . $class['logo'];
                        }
                    }
                }
            }
        }

        $this->success('', $list);
    }

    /**
     * @ApiTitle    (老师列表)
     * @ApiAuthor   (Zhenzh)
     * @ApiDate     (2020/2/20)
     * @ApiSummary  (描述)
     * @ApiBody     (Body)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/school/teachers)
     * @ApiParams   (name="id", type="int", required=true, description="学校ID", sample="36")
     * @ApiParams   (name="page", type="int", required=false, description="页码 默认不分页0", sample="0")
     * @ApiParams   (name="keywords", type="string", required=false, description="搜索关键词", sample="")
     * @ApiReturnParams   (name="code", type="integer", required=true, description="状态码 1正常 0错误", sample="1")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回", sample="")
     * @ApiReturn ({
    "code": 1,
    "msg": "",
    "time": "1582190994",
    "data": {
    "total": 1,
    "per_page": 15,
    "current_page": 1,
    "last_page": 1,
    "data": [
    {
    "id": 42,
    "user_id": 58,
    "card_number": "20191001",
    "created_at": 1573873413,
    "school_id": 36,
    "school_id": 3,
    "username": "18911621396",
    "nickname": "test",
    "mobile": "18911621396",
    "avatar": "http://mp.uxuj.test/uploads/20200109/2863d0a1ff4b87ba621cb84637c3796d.jpg"
    }
    ]
    }
    })
     */
    public function teachers()
    {
        $id = $this->request->request('id', 0);
        $page = $this->request->request('page', 0);
        $keywords = $this->request->request('keywords', '');

        if (!$id) {
            $this->error('参数有误');
        }

        if ($page) {
            $list = \app\common\model\Teacher::alias('teacher')
                ->join('__USER__ user', 'user.id = teacher.user_id')
                ->where('teacher.school_id', $id)
                ->where('user.nickname|user.mobile', 'like', "%$keywords%")
                ->field('teacher.*,user.username,user.nickname,user.mobile,user.avatar')
                ->paginate();

            if ($list) {
                $list = $list->toArray();
                foreach ($list['data'] as $key => $datum) {
                    if (!strstr($datum['avatar'], 'http') && strstr($datum['avatar'], '/uploads/')) {
                        $list['data'][$key]['avatar'] = config('app_url') . $datum['avatar'];
                    }
                }
            }
        } else {
            $list = \app\common\model\Teacher::alias('teacher')
                ->join('__USER__ user', 'user.id = teacher.user_id')
                ->where('teacher.school_id', $id)
                ->where('user.nickname|user.mobile', 'like', "%$keywords%")
                ->field('teacher.*,user.username,user.nickname,user.mobile,user.avatar')
                ->select();
        }

        $this->success('', $list);

    }

}
